package com.hrd.admin.system.inventory;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hrd.admin.base.BaseController;
import com.hrd.admin.base.BaseDefine;
import com.hrd.common.util.bean.ServiceResult;
import com.hrd.mybatis.bean.ext.ExtWarehouse;
import com.hrd.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: wangpeng
 * @Date: 2021/11/3 13:48
 * @Description:
 */
@Controller
@RequestMapping("/system/inventory")
public class InventoryController extends BaseController {
    @Autowired
    public StatisticsService statisticsService;
    /**
     * 跳转统计页面
     * @return
     */
    @RequestMapping("init")
    public ModelAndView init() {
        ModelAndView modelAndView = new ModelAndView("/system/statistics/inventory_list");
        return modelAndView;

    }

    @RequestMapping(value="searchAction")//, produces = MediaType.APPLICATION_JSON_VALUE
    public JSONObject searchAction(HttpServletRequest request,@ModelAttribute(BaseDefine.FORM) ExtWarehouse form) {
        // 创建分页
        return this.createPage(request,form);
    }

    /**
     * 创建分页机能
     *
     * @param request
     * @param form
     */
    private JSONObject createPage(HttpServletRequest request,ExtWarehouse form) {

        JSONObject object = new JSONObject();
        // 翻页
        ServiceResult<List<HashMap<String, String>>> result = statisticsService.statisticsInv(form,false);

        if (result != null) {
            List<HashMap<String, String>> recordList = result.getResult();
            PageInfo paginator = new PageInfo(recordList);// 默认8页数据
            object.put("rows", paginator.getList());
            object.put("total", paginator.getTotal());
        }

        return object;
    }

    @ResponseBody
    @RequestMapping("getListAction")
    public List<JSONObject> getListAction(HttpServletRequest request, @ModelAttribute(BaseDefine.FORM) ExtWarehouse form) {
        return statisticsService.getcolumns(form,false);
    }
}
